package ircbot.app;

import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.util.regex.*;


public class IRC_Client {

    public static void main(String[] args) throws IOException {

        Scanner in = new Scanner(System.in);
        
        //connection variables
        String server = "irc.snappeh.com";
        String nick = "lkc";
        String login = "lkc";
        String channel = "#bots";
        int port = 6667;


        try {

            //our socket we're connected with
            Socket irc = new Socket(server, port);
            //out output stream
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(irc.getOutputStream()));
            //our input stream
            BufferedReader br = new BufferedReader(new InputStreamReader(irc.getInputStream()));

            //authenticate with the server
            bw.write("NICK " + nick + "\n");
            bw.write("USER " + login + " java.sun.com USR: KUHN\n");
            bw.flush();

            //join a channel
            bw.write("JOIN " + channel + "\n");
            bw.flush();

            System.out.println("Successfully connected");



            IRCBackgroundThread BgThread = new IRCBackgroundThread(bw, br, channel);
            
            while(true){
                
                String cmd = in.nextLine();
                
                
                if(cmd.equalsIgnoreCase("quit")){
                    
                    BgThread.setKeepAlive(false);                    
                    break;
                }else{
                    
                    //bw.write("PRIVMSG " + channel + " :" + cmd + "\n");
                    bw.write("PRIVMSG " + cmd + "\n");
                    bw.flush();
                }
                
            }
                 
            

            
        } catch (UnknownHostException e) {
            System.err.println("No such host");
        } catch (IOException e) {
            System.err.println("There was an error connecting to the host");
        }
    }
}

class IRCBackgroundThread extends Thread {

    private BufferedWriter bw;
    private BufferedReader br;
    private String channel;
    private boolean keepAlive;

    IRCBackgroundThread(BufferedWriter bw, BufferedReader br, String channel) {


        this.bw = bw;
        this.br = br;
        this.channel = channel;
        this.keepAlive = true;

        start();
    }

    public void run() {

        try {

            String currLine = null;
            while (keepAlive && (currLine = br.readLine()) != null){

                //checks for PING, if one is found; return a PONG
                Pattern pingRegex = Pattern.compile("^PING", Pattern.CASE_INSENSITIVE);
                Matcher ping = pingRegex.matcher(currLine);
                if (ping.find()) {

                    bw.write("PONG " + channel + "\n");
                    bw.flush();
                } else {

                    System.out.println(currLine);
                }
            }
            
        }catch (IOException e) {
            System.err.println("There was an error connecting to the host");
        }

    }
    
    public void setKeepAlive(boolean keepAlive){
    
        this.keepAlive = keepAlive;
    }
    
    public boolean getKeepAlive(){
    
        return this.keepAlive;
    }
    
}
       



